自动生成损益单

--select profitloss_bi from profitloss_m where memo like '库存结转自动生成损益单-201305%'
--603100000134
--begin tran
declare @dt varchar(8)
select @dt='20130501'

declare @memo varchar(50),
@profit_bi varchar(12),
@outlet_id varchar(8)

select @memo='调整4月份结存历史误差-'+@dt


select @outlet_id=ltrim(rtrim(run_values)) from setting where name='outlet_id'
select @profit_bi = '6'+@outlet_id+ RIGHT('00000000' + CONVERT(VARCHAR(8),CONVERT(NUMERIC(8,0),NUMBER)+1),8)
from number where number_id ='pftloss'

update number set number = CONVERT(VARCHAR(8),CONVERT(NUMERIC(8,0),NUMBER)+1) where number_id = 'pftloss'
insert into profitloss_m(profitloss_bi, outlet_id,headquarters_id,folio_ref, input_dt, inputter, audite_dt, auditer, status, memo)
values(@profit_bi,@outlet_id,'H01', '',@dt,
'系统生成',@dt,'系统生成','1',@memo)



insert into profitloss_d(profitloss_bi,aid,storespace_id,q,entry_a,memo)
select @profit_bi,aa.aid,'00',0,-(a1-a2) as entry_a,''
from
(
select a.aid,sum(tax_entry_a) as a1
,sum(stock_q) q1
from article_storespace b,article a
where a.aid=b.aid and left(a.segregate_id,2)='01'
group by a.aid
)aa,
(
select sum(q) q2,
a.aid,
sum(ea) as a2 from dbo.uf_get_stock_da_curr('20130527') a,article b
where a.aid=b.aid and left(b.segregate_id,2)='01'
group by a.aid
)bb,article c
where aa.aid=bb.aid and aa.aid=c.aid and abs(a1-a2)>0
order by abs(a1-a2) desc

print @profit_bi


--rollback

select * from profitloss_m where profitloss_bi='603100000137'
select * from profitloss_d where profitloss_bi='603100000137'

posted @ 2013-05-27 14:53  kuailewangzi1212  阅读(198)  评论(0编辑  收藏  举报