/* select * from import_cargo select * from code_currency --select * from sys_module where description='费用代码设置' select * from ch_fee where bsno='AA01' delete from ch_fee where bsno='AA01' and feename in('购货款','预付款','尾款','关税','增值税') select c.id,currid,c.name,price,[weight],price_limit,codename, baolv,prepayments,balance,tax,tax_zz,tariff,m.seller,M.buyer from import_cargo c left join import_main M on m.contractno=c.contractno left join code_currency cu on cu.gid=c.currid where c.contractno='AA01' */ ----------------- IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sMsCWStart]') AND type in (N'P', N'PC')) DROP PROCEDURE [dbo].[sMsCWStart] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO Create PROCEDURE [dbo].[sMsCWStart] (@contractno varchar(50) ) --WITH ENCRYPTION AS declare @id int declare @name varchar(50) declare @currid varchar(50) declare @price numeric(18,3) declare @price_agio numeric(18,3) declare @weight numeric(18,3) declare @price_limit numeric(18,3) declare @baolv numeric(8,4) declare @prepayments numeric(18,3) declare @balance numeric(18,3) declare @tariff varchar(20) declare @tax numeric(8,3) declare @tax_zz numeric(8,3) declare @newgid varchar(50) declare @seller varchar(50) declare @buyer varchar(50) declare @codename varchar(50) declare @H numeric(18,3) declare @W numeric(18,3) declare @paypart tinyint declare @exchangerate numeric(18,4) --declare @contractno varchar(50) --set @contractno='A1' Begin --delete from ch_fee where bsno=@contractno --and feename in('购货款','预付款','尾款','关税','增值税') declare cursor1 cursor for --定义游标cursor1 --使用游标的对象(跟据需要填入select文) select c.id,currid,c.name,price,price_agio,[weight], price_limit,codename,baolv,prepayments,balance,tax,tax_zz,tariff, m.seller,M.buyer,paypart,exchangerate from import_cargo c left join import_main M on m.contractno=c.contractno left join code_currency cu on cu.gid=c.currid where c.contractno=@contractno open cursor1 fetch next from cursor1 into @id,@currid,@name,@price,@price_agio,@weight, @price_limit,@codename,@baolv,@prepayments,@balance,@tax,@tax_zz,@tariff, @seller,@buyer,@paypart,@exchangerate while @@fetch_status=0 --判断是否成功获取数据 begin set @H=@price*@weight if (@price_limit=0) begin set @W=@H*@baolv end if (@price_limit=1) begin set @W=@price_limit*@weight*@baolv end --select @H,@W set @newgid=(select newid()) insert into ch_fee --插入chfee 购货款 (GID,bsno,feetype,feename,customername,unitprice, quantity,amount, currency,SUBMITDATE,ExChangerate,remark)values (@newgid,@contractno,2,'购货款',@seller,@price-@price_agio, @weight,(@price-@price_agio)*@weight*@exchangerate, @codename,getdate(),@exchangerate,@name+' 购货价:(单价'+convert(varchar(20),@price) +'-差价'+convert(varchar(20),@price_agio)+')×净重'+convert(varchar(20),@weight)+'kg 付给贸易商') set @newgid=(select newid()) insert into ch_fee --插入chfee 关税 (GID,bsno,feetype,feename,customername,unitprice, quantity,amount,currency,SUBMITDATE,ExChangerate,remark)values ( @newgid,@contractno,1,'关税',@buyer,@W, @Tax,@W*@Tax*@exchangerate,@codename,getdate(),@exchangerate,@name+' 合同金额'+CONVERT(varchar(20),@H) +' 保率'+CONVERT(varchar(20),@baolv)+' 完税价格'+CONVERT(varchar(20),@W) +' '+@tariff+'税率:'+CONVERT(varchar(10),@tax*100)+'%') set @newgid=(select newid()) insert into ch_fee --插入chfee 关税 (GID,bsno,feetype,feename,customername,unitprice, quantity,amount,currency,SUBMITDATE,ExChangerate,remark)values ( @newgid,@contractno,2,'关税','海关',@W, @Tax,@W*@Tax*@exchangerate,@codename,getdate(),@exchangerate,@name+' 合同金额'+CONVERT(varchar(20),@H) +' 保率'+CONVERT(varchar(20),@baolv)+' 完税价格'+CONVERT(varchar(20),@W) +' '+@tariff+'税率:'+CONVERT(varchar(10),@tax*100)+'%') set @newgid=(select newid()) insert into ch_fee --插入chfee 增值税 (GID,bsno,feetype,feename,customername,unitprice, quantity,amount,currency,SUBMITDATE,ExChangerate,remark)values ( @newgid,@contractno,1,'增值税',@buyer,@W*(1+@tax), @Tax_zz,@W*(1+@tax)*@Tax_zz*@exchangerate,@codename,getdate(),@exchangerate,@name+' 完税价格'+CONVERT(varchar(20),@W) +' 关税' +CONVERT(varchar(50),@W*@tax) +' 增值税率:'+CONVERT(varchar(10),@tax_zz*100)+'%') set @newgid=(select newid()) insert into ch_fee --插入chfee 增值税 (GID,bsno,feetype,feename,customername,unitprice, quantity,amount,currency,SUBMITDATE,ExChangerate,remark)values ( @newgid,@contractno,2,'增值税','海关',@W*(1+@tax), @Tax_zz,@W*(1+@tax)*@Tax_zz*@exchangerate,@codename,getdate(),@exchangerate,@name+' 完税价格'+CONVERT(varchar(20),@W) +' 关税' +CONVERT(varchar(50),@W*@tax) +' 增值税率:'+CONVERT(varchar(10),@tax_zz*100)+'%') if ((@paypart=0) or (@paypart=1)) begin set @newgid=(select newid()) insert into ch_fee --插入chfee 预付款 (GID,bsno,feetype,feename,customername,unitprice, quantity,amount,currency,SUBMITDATE,ExChangerate,remark)values (@newgid,@contractno,1,'预付款',@buyer,@prepayments, 1,@prepayments*@exchangerate,@codename,getdate(),@exchangerate,@name+' 客户付给') end if ((@paypart=0) or (@paypart=3)) begin set @newgid=(select newid()) insert into ch_fee --插入chfee 尾款 (GID,bsno,feetype,feename,customername,unitprice, quantity,amount,currency,SUBMITDATE,ExChangerate,remark)values (@newgid,@contractno,1,'尾款',@buyer,@balance, 1,@balance*@exchangerate,@codename,getdate(),@exchangerate,@name+' 客户付给') end fetch next from cursor1 into @id,@currid,@name,@price,@price_agio,@weight, @price_limit,@codename,@baolv,@prepayments,@balance,@tax,@tax_zz,@tariff, @seller,@buyer,@paypart,@exchangerate end close cursor1 --关闭游标 deallocate cursor1 End GO --exec smsCWStart 'A1'