--存货引入库存期初
declare @cwhcode as nvarchar(10)
set @cwhcode='1'
SELECT RdRecord.brdflag, RdRecord.cBustype, RdRecord.cBuscode,RdRecord.cCode as cVouCode, RdRecords.AutoID as ID,Null as ValueId,Null as JustId,RdRecord.dDate as dVoudate,null as dKeepDate,0 as imonth,null as IPzId,null as iPzDate, null as cPzType,null as cPzdigest,null as cInvHead,null as cDifHead,null as cOppHead,RdRecord.cVouchType as cVouType, RdRecord.cPtCode,RdRecord.cStCode,RdRecord.cWhCode,RdRecord.cDepCode as cAccdep, RdRecords.cInvCode, RdRecord.cRdCode, RdRecord.cVenCode,RdRecord.cCusCode,null as cOrderCode,null as cArvCode,null as cbillcode,null as cDlcode,null as cpspCode,Null as cProcode,null as cdepcode,RdRecord.cPersonCode,RdRecord.cHandler,RdRecords.iQuantity As iAinQuantity, null as iAoutquantity,RdRecords.iUnitCost as iInCost,null as iOutCost,RdRecords.iPrice as iaInPrice,null as iaOutPrice,null as cBathcode,null as iDebitDifCost,null as iCreditDifCost,null as caccounter,RdRecord.cMaker,0 as bflag,RdRecords.iflag as bMoneyFlag, 0 as bSale,RdRecord.cmemo,RdRecord.cDefine1,RdRecord.cDefine2,RdRecord.cDefine3,RdRecord.cDefine4,RdRecord.cDefine5,RdRecord.cDefine6,RdRecord.cDefine7,RdRecord.cDefine8,RdRecord.cDefine9,RdRecord.cDefine10,RdRecord.cDefine11,RdRecord.cDefine12,RdRecord.cDefine13,RdRecord.cDefine14,RdRecord.cDefine15,RdRecord.cDefine16,RdRecords.cFree1,RdRecords.cFree2,RdRecords.cFree3,RdRecords.cFree4,RdRecords.cFree5,RdRecords.cFree6,RdRecords.cFree7,RdRecords.cFree8,RdRecords.cFree9,RdRecords.cFree10,Rdrecords.cDefine22 ,Rdrecords.cDefine23 ,Rdrecords.cDefine24 ,Rdrecords.cDefine25 ,Rdrecords.cDefine26 ,Rdrecords.cDefine27,Rdrecords.cDefine28 ,Rdrecords.cDefine29 ,Rdrecords.cDefine30 ,Rdrecords.cDefine31 ,Rdrecords.cDefine32 ,Rdrecords.cDefine33,Rdrecords.cDefine34 ,Rdrecords.cDefine35 ,Rdrecords.cDefine36 ,Rdrecords.cDefine37 ,RdRecords.citem_class,RdRecords.citemcode,RdRecords.citemCname,RdRecords.cName,null as cPzid,null as nojustquantity into tempKcqc0 From RdRecord left join rdrecords ON rdrecords.id=rdrecord.id left join Warehouse ON Warehouse.cWhCode = RdRecord.cWhCode
WHERE RdRecord.cVouchType='34' and RdRecord.cwhcode=@cwhcode
go
alter table tempKcqc0 alter column iDebitDifCost float
go
alter table tempKcqc0 alter column iInCost float
go
alter table tempKcqc0 alter column iaInPrice float
go
select distinct cwhcode,cinvcode , sign(isnull(iainquantity,0)+isnull(iainprice,0)) as bsign into [TempSignDJMOVE0] from ia_subsidiary where Ia_subsidiary.cVouType='34' and Ia_subsidiary.cwhcode='1' and (isnull(IA_Subsidiary.cBusType,'') not in('假退料'))
go
alter table tempKcqc0 add autoid int identity
go
delete from tempKcqc0 where autoid in (select tempKcqc0.autoid from tempKcqc0 left join warehouse on warehouse.cwhcode=tempKcqc0.cwhcode left join [TempSignDJMOVE0] on ([TempSignDJMOVE0].cwhcode=tempKcqc0.cwhcode and [TempSignDJMOVE0].cinvcode=tempKcqc0.cinvcode) where ((isnull(bsign,0)*(isnull(tempKcqc0.iainquantity,0)+isnull(tempKcqc0.iainprice,0))<0 or (bsign is null and tempKcqc0.iainquantity<0 and exists(select tempKcqc0.cwhcode from tempKcqc0 tempKcqc00 where tempKcqc00.cwhcode=tempKcqc0.cwhcode and tempKcqc00.cinvcode=tempKcqc0.cinvcode and sign(tempKcqc00.iainquantity) > 0) )and (cwhvaluestyle='先进先出法' or cwhvaluestyle='后进先出法')) or (cwhvaluestyle='个别计价法' and isnull(tempKcqc0.iainquantity,0)<=0 )))
go
alter table tempKcqc0 drop column autoid
go
drop table [TempSignDJMOVE0]
go
--清空数据
delete from ia_subsidiary
--插入数据
Insert into Ia_Subsidiary (brdflag,cBustype,cBuscode,cVouCode,ID,ValueId,JustId,dVoudate,dKeepDate, imonth,IPzId,iPzDate,cPzType,cPzdigest,cInvHead,cDifHead,cOppHead, cVouType,cPtCode,cStCode,cWhCode,cAccdep,cInvCode,cRdCode,cVenCode, cCusCode,cOrderCode,cArvCode,cbillcode,cDlcode,cpspCode,cProcode,cDepCode,cPersonCode,cHandler, iAinQuantity,iAoutquantity,iInCost,iOutCost,iaInPrice,iaOutPrice,cBatchcode,iDebitDifCost,iCreditDifCost,caccounter, cMaker,bflag,bMoneyFlag,bSale,cmemo, cDefine1,cDefine2,cDefine3,cDefine4,cDefine5,cDefine6,cDefine7,cDefine8,cDefine9,cDefine10,cDefine11,cDefine12, cDefine13,cDefine14,cDefine15,cDefine16, cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10, cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27, cDefine28 ,cDefine29 ,cDefine30 ,cDefine31 ,cDefine32 ,cDefine33, cDefine34 ,cDefine35 ,cDefine36 ,cDefine37 , citem_class,citemcode,citemCname,cName, cPzid, nojustquantity )
select * from tempKcqc0
go
drop table tempKcqc0
go
update ia_subsidiary set IaInPrice=convert(decimal(20,2),convert(decimal(20,2),iInvRCost)*convert(decimal(20,2),iAInQuantity)),IInCost= iinvRcost from ia_subsidiary left join inventory on inventory.cinvcode=ia_subsidiary.cinvcode left join warehouse on warehouse.cwhcode=ia_subsidiary.cwhcode where imonth=0 and (cwhvaluestyle='计划价法' or cwhvaluestyle='售价法')