select * from t_TableDescription where FTableName= 'ICCreditBill'
select * from t_TableDescription where fdescription like '%采购发票%'
select * from t_FieldDescription where FTableID=60038 order by FFieldName
select * from t_TableDescription where fdescription like '%入库%'
select * from t_FieldDescription where FTableID=210009 order by FFieldName
select * from t_TableDescription where ftablename like '%t_rpcontract%' --合同表(应付/应收)
select * from t_FieldDescription where FTableID=50030 order by FFieldName
select * from ICPurchase
--专票
select FPeriod,FYear from T_PeriodDate where '2018-04-10'>=FStartDate and '2018-04-10'<=FEndDate
declare @p2 int
set @p2=1204
exec GetICMaxNum 'ICPurchase',@p2 output,1,16394
select @p2
INSERT INTO ICPurchaseEntry
(FInterID,FEntryID,FBrNo,FMapNumber,FMapName,
FItemID,FAuxPropID,FQty,FUnitID,Fauxqty,
FSecCoefficient,FSecQty,Fauxprice,FAuxTaxPrice,FDiscountRate,
FAuxPriceDiscount,Famount,FStdAmount,FAmtDiscount,FStdAmtDiscount,
FTaxRate,FTaxAmount,FStdTaxAmount,FAmountMust,FAmountMustOld,
FNoMust,FNoMustOld,FDeductTax,FDeductTaxOld,FOrgBillEntryID,
FOrderPrice,FAuxOrderPrice,FClassID_SRC,FEntryID_SRC,FSourceBillNo,
FSourceTranType,FSourceInterId,FSourceEntryID,FContractBillNo,FContractInterID,
FContractEntryID,FOrderBillNo,FOrderInterID,FOrderEntryID,FAllHookQTY,
FStdAllHookAmount,FCurrentHookQTY,FStdCurrentHookAmount,FPlanMode,FMTONo,
FOrderType,FBatchNo,FItemStatementBillNO,FItemStatementEntryID,FItemStatementInterID)
SELECT 1204,1,'0','','',604,0,3418,161,3418,0,0,36.504274,42.71,0,42.71,124771.61,124771.61,0,0,17,21211.17,21211.17,0,0,0,0,0,0,0,42.71,42.71,0,0,'WIN000085',1,2199,1,'',0,0,'POORD000080',1227,1,0,0,0,0,14036,'',71,'','',0,0 union all
SELECT 1204,2,'0','','',609,0,890,161,890,0,0,32.435897,37.95,0,37.95,28867.95,28867.95,0,0,17,4907.55,4907.55,0,0,0,0,0,0,0,37.95,37.95,0,0,'WIN000085',1,2199,2,'',0,0,'POORD000080',1227,2,0,0,0,0,14036,'',71,'','',0,0 union all
SELECT 1204,3,'0','','',609,0,140,161,140,0,0,32.435897,37.95,0,37.95,4541.03,4541.03,0,0,17,771.97,771.97,0,0,0,0,0,0,0,37.95,37.95,0,0,'WIN000085',1,2199,3,'',0,0,'POORD000080',1227,3,0,0,0,0,14036,'',71,'','',0,0 union all
SELECT 1204,4,'0','','',611,0,1670,161,1670,0,0,30.820513,36.06,0,36.06,51470.26,51470.26,0,0,17,8749.94,8749.94,0,0,0,0,0,0,0,36.06,36.06,0,0,'WIN000085',1,2199,4,'',0,0,'POORD000080',1227,4,0,0,0,0,14036,'',71,'','',0,0 union all
SELECT 1204,5,'0','','',611,0,468,161,468,0,0,30.820513,36.06,0,36.06,14424,14424,0,0,17,2452.08,2452.08,0,0,0,0,0,0,0,36.06,36.06,0,0,'WIN000085',1,2199,5,'',0,0,'POORD000080',1227,5,0,0,0,0,14036,'',71,'','',0,0
EXEC p_UpdateBillRelateData 75,1204,'ICPurchase','ICPurchaseEntry'
INSERT INTO ICPurchase
(FInterID,FBillNo,FBrNo,FTranType,FCancellation,
FStatus,FROB,FClassTypeID,FSubSystemID,FYear,
FPeriod,FItemClassID,FFincDate,FHookStatus,FTotalCostFor,
FTotalCost,Fdate,FSupplyID,FTaxNum,FCheckDate,
FDeptID,FEmpID,FBillerName,FCurrencyID,FInvStyle,
FExchangeRateType,FExchangeRate,FCompactNo,Fnote,FBillerID,
FPOStyle,FYearPeriod,FMultiCheckDate1,FMultiCheckDate2,FPOOrdBillNo,
FMultiCheckDate3,FMultiCheckDate4,FMultiCheckDate5,FMultiCheckDate6,FPrintCount,
FYtdIntRate,FAcctID,FOrgBillInterID,FHookerID,FSelTranType,
FBrID,FManagerID,FCussentAcctID,FPayCondition,FSettleDate,FSysStatus)
SELECT 1204,'ZPOFP000095','0',75,0,
0,1,1000004,0,2018,
4,8,'2018-04-10',0,262167.56,
262167.56,'2018-04-10',412,'',Null,
241,2188,'',1,12510,
1,1,'','',16394,
252,'',Null,Null,'',
Null,Null,Null,Null,0,
0,0,0,0,1,
0,0,0,1006,'2018-05-08',0
UPDATE ICPurchase SET FSysStatus = 2 WHERE FInterID = 1204
UPDATE ICPurchase SET FUUID=NEWID() WHERE FInterID=1204
--更新采购入库单的开票数量
set nocount on
declare @fcheck_fail int
declare @fsrccommitfield_prevalue decimal(28,13)
declare @fsrccommitfield_endvalue decimal(28,10)
declare @maxorder int
update src set @fsrccommitfield_prevalue= isnull(src.fqtyinvoice,0),
@fsrccommitfield_endvalue=@fsrccommitfield_prevalue+dest.fqty,
@fcheck_fail=case isnull(@maxorder,0) when 1 then 0 else (case when (abs(src.fqty)>abs(@fsrccommitfield_prevalue) or abs(src.fqty)>abs(@fsrccommitfield_endvalue)) then @fcheck_fail else -1 end) end,
src.fqtyinvoice=@fsrccommitfield_endvalue, --基本单位开票数量
src.fauxqtyinvoice=@fsrccommitfield_endvalue/cast(t1.fcoefficient as float) --开票数量
from icstockbillentry src
inner join icstockbill srchead on src.finterid=srchead.finterid
inner join
(select u1.fsourceinterid as fsourceinterid,u1.fsourceentryid,u1.fitemid,sum(u1.fqty) as fqty
from icpurchaseentry u1
where u1.finterid=1204
group by u1.fsourceinterid,u1.fsourceentryid,u1.fitemid) dest
on dest.fsourceinterid = src.finterid
and dest.fitemid = src.fitemid
and src.fentryid = dest.fsourceentryid
inner join t_measureunit t1 on src.funitid=t1.fitemid
if (isnull(@fcheck_fail,0)=-1)
raiserror('可能的原因是:
1、所选单据已被其他单据关联
2、所选单据已被反审核
3、当前单据和所选单据的关联数量超过了所选单据的数量
4、所选单据已经关闭',18,18)
else
if exists (select 1 from icstockbill src right join (select u1.fsourceinterid as fsourceinterid,u1.fsourceentryid,u1.fitemid,sum(u1.fqty) as fqty
from icpurchaseentry u1
where u1.finterid=1204
group by u1.fsourceinterid,u1.fsourceentryid,u1.fitemid) dest on dest.fsourceinterid = src.finterid where dest.fsourceinterid>0 and src.finterid is null)
raiserror('所选单据已被删除',18,18)
--更新采购订单的开票数量
set nocount on
declare @fcheck_fail int
declare @fsrccommitfield_prevalue decimal(28,13)
declare @fsrccommitfield_endvalue decimal(28,10)
declare @maxorder int
update src set @fsrccommitfield_prevalue= isnull(src.fqtyinvoice,0),
@fsrccommitfield_endvalue=@fsrccommitfield_prevalue+dest.fqty,
@fcheck_fail=case isnull(@maxorder,0) when 1 then 0 else (case when (1=1) then @fcheck_fail else -1 end) end,
src.fqtyinvoice=@fsrccommitfield_endvalue,
src.fauxqtyinvoice=@fsrccommitfield_endvalue/cast(t1.fcoefficient as float)
from poorderentry src
inner join poorder srchead on src.finterid=srchead.finterid
inner join
(select u1.forderinterid as fsourceinterid,u1.forderentryid,u1.fitemid,sum(u1.fqty) as fqty
from icpurchaseentry u1
where u1.finterid=1204
group by u1.forderinterid,u1.forderentryid,u1.fitemid) dest
on dest.fsourceinterid = src.finterid
and dest.fitemid = src.fitemid
and src.fentryid = dest.forderentryid
inner join t_measureunit t1 on src.funitid=t1.fitemid
--更新合同(应付)的开票数量
set nocount on
declare @fcheck_fail int
declare @fsrccommitfield_prevalue decimal(28,13)
declare @fsrccommitfield_endvalue decimal(28,10)
declare @maxorder int
update src set @fsrccommitfield_prevalue= isnull(abs(src.finvoiceqty_relative_base),0),
@fsrccommitfield_endvalue=@fsrccommitfield_prevalue+dest.fqty,
@fcheck_fail=case isnull(@maxorder,0) when 1 then 0 else (case when (1=1) then @fcheck_fail else -1 end) end,
src.finvoiceqty_relative_base= case when src.fquantity_base < 0 then -1 * @fsrccommitfield_endvalue else @fsrccommitfield_endvalue end,
src.finvoiceqty_relative= case when src.fquantity_base < 0 then -1 * (@fsrccommitfield_endvalue/cast(t1.fcoefficient as float)) else @fsrccommitfield_endvalue/cast(t1.fcoefficient as float) end
from t_rpcontractentry src
inner join t_rpcontract srchead on src.fcontractid=srchead.fcontractid
inner join
(select u1.fcontractinterid as fsourceinterid,u1.fcontractentryid,u1.fitemid,sum(u1.fqty) as fqty
from icpurchaseentry u1
where u1.finterid=1204
group by u1.fcontractinterid,u1.fcontractentryid,u1.fitemid) dest
on dest.fsourceinterid = src.fcontractid
and dest.fitemid = src.fproductid
and src.fentryid = dest.fcontractentryid
inner join t_measureunit t1 on src.funitid=t1.fitemid where (select fclasstypeid from t_rpcontract where fcontractid=src.fcontractid)=1000020
--更新合同(应付)的发票关联金额(本位币)
set nocount on
declare @fcheck_fail int
declare @fsrccommitfield_prevalue decimal(28,13)
declare @fsrccommitfield_endvalue decimal(28,10)
declare @maxorder int
update src set @fsrccommitfield_prevalue= isnull(abs(src.finvoiceamt_relative),0),
@fsrccommitfield_endvalue=@fsrccommitfield_prevalue+dest.fstdamountincludetax,
@fcheck_fail=case isnull(@maxorder,0) when 1 then 0 else (case when (1=1) then @fcheck_fail else -1 end) end,
src.finvoiceamt_relative= case when src.famountincludetax < 0 then -1 * @fsrccommitfield_endvalue else @fsrccommitfield_endvalue end
from t_rpcontractentry src
inner join t_rpcontract srchead on src.fcontractid=srchead.fcontractid
inner join
(select u1.fcontractinterid as fsourceinterid,u1.fcontractentryid,u1.fitemid,sum(u1.fstdamountincludetax) as fstdamountincludetax
from icpurchaseentry u1
where u1.finterid=1204
and not exists (select 1 from icbillrelations_purchase tr where u1.finterid = tr.fbillid and u1.fentryid=tr.fdestentryid) and not exists (select 1 from icbillrelations_purchaser tr where u1.finterid = tr.fbillid and u1.fentryid=tr.fdestentryid) group by u1.fcontractinterid,u1.fcontractentryid,u1.fitemid) dest
on dest.fsourceinterid = src.fcontractid
and dest.fitemid = src.fproductid
and src.fentryid = dest.fcontractentryid
where (select fclasstypeid from t_rpcontract where fcontractid=src.fcontractid)=1000020
--更新合同(应付)的发票关联金额
set nocount on
declare @fcheck_fail int
declare @fsrccommitfield_prevalue decimal(28,13)
declare @fsrccommitfield_endvalue decimal(28,10)
declare @maxorder int
update src set @fsrccommitfield_prevalue= isnull(abs(src.finvoiceamtfor_relative),0),
@fsrccommitfield_endvalue=@fsrccommitfield_prevalue+dest.famountincludetax,
@fcheck_fail=case isnull(@maxorder,0) when 1 then 0 else (case when (1=1) then @fcheck_fail else -1 end) end,
src.finvoiceamtfor_relative= case when src.famountincludetax < 0 then -1 * @fsrccommitfield_endvalue else @fsrccommitfield_endvalue end
from t_rpcontractentry src
inner join t_rpcontract srchead on src.fcontractid=srchead.fcontractid
inner join
(select u1.fcontractinterid as fsourceinterid,u1.fcontractentryid,u1.fitemid,sum(u1.famountincludetax) as famountincludetax
from icpurchaseentry u1
where u1.finterid=1204
and not exists (select 1 from icbillrelations_purchase tr where u1.finterid = tr.fbillid and u1.fentryid=tr.fdestentryid) and not exists (select 1 from icbillrelations_purchaser tr where u1.finterid = tr.fbillid and u1.fentryid=tr.fdestentryid) group by u1.fcontractinterid,u1.fcontractentryid,u1.fitemid) dest
on dest.fsourceinterid = src.fcontractid
and dest.fitemid = src.fproductid
and src.fentryid = dest.fcontractentryid
where (select fclasstypeid from t_rpcontract where fcontractid=src.fcontractid)=1000020
--更新合同(应付)的发票关联数量、发票关联基本数量
set nocount on
declare @fcheck_fail int
declare @fsrccommitfield_prevalue decimal(28,13)
declare @fsrccommitfield_endvalue decimal(28,10)
declare @maxorder int
update src set @fsrccommitfield_prevalue= isnull(abs(src.finvoiceqty_relative_base),0),
@fsrccommitfield_endvalue=@fsrccommitfield_prevalue+dest.fqty,
@fcheck_fail=case isnull(@maxorder,0) when 1 then 0 else (case when (1=1) then @fcheck_fail else -1 end) end,
src.finvoiceqty_relative_base= case when src.fquantity_base < 0 then -1 * @fsrccommitfield_endvalue else @fsrccommitfield_endvalue end,
src.finvoiceqty_relative= case when src.fquantity_base < 0 then -1 * (@fsrccommitfield_endvalue/cast(t1.fcoefficient as float)) else @fsrccommitfield_endvalue/cast(t1.fcoefficient as float) end
from t_rpcontractentry src
inner join t_rpcontract srchead on src.fcontractid=srchead.fcontractid
inner join
(select u1.fcontractinterid as fsourceinterid,u1.fcontractentryid,u1.fitemid,sum(u1.fqty) as fqty
from icpurchaseentry u1
where u1.finterid=1204
group by u1.fcontractinterid,u1.fcontractentryid,u1.fitemid) dest
on dest.fsourceinterid = src.fcontractid
and dest.fitemid = src.fproductid
and src.fentryid = dest.fcontractentryid
inner join t_measureunit t1 on src.funitid=t1.fitemid where (select fclasstypeid from t_rpcontract where fcontractid=src.fcontractid)=1000551
--更新初始化合同(应付)的发票关联金额
set nocount on
declare @fcheck_fail int
declare @fsrccommitfield_prevalue decimal(28,13)
declare @fsrccommitfield_endvalue decimal(28,10)
declare @maxorder int
update src set @fsrccommitfield_prevalue= isnull(abs(src.finvoiceamt_relative),0),
@fsrccommitfield_endvalue=@fsrccommitfield_prevalue+dest.fstdamountincludetax,
@fcheck_fail=case isnull(@maxorder,0) when 1 then 0 else (case when (1=1) then @fcheck_fail else -1 end) end,
src.finvoiceamt_relative= case when src.famountincludetax < 0 then -1 * @fsrccommitfield_endvalue else @fsrccommitfield_endvalue end
from t_rpcontractentry src
inner join t_rpcontract srchead on src.fcontractid=srchead.fcontractid
inner join
(select u1.fcontractinterid as fsourceinterid,u1.fcontractentryid,u1.fitemid,sum(u1.fstdamountincludetax) as fstdamountincludetax
from icpurchaseentry u1
where u1.finterid=1204
and not exists (select 1 from icbillrelations_purchase tr where u1.finterid = tr.fbillid and u1.fentryid=tr.fdestentryid) and not exists (select 1 from icbillrelations_purchaser tr where u1.finterid = tr.fbillid and u1.fentryid=tr.fdestentryid) group by u1.fcontractinterid,u1.fcontractentryid,u1.fitemid) dest
on dest.fsourceinterid = src.fcontractid
and dest.fitemid = src.fproductid
and src.fentryid = dest.fcontractentryid
where (select fclasstypeid from t_rpcontract where fcontractid=src.fcontractid)=1000551
--更新初始化合同(应付)的发票关联金额(本位币)
set nocount on
declare @fcheck_fail int
declare @fsrccommitfield_prevalue decimal(28,13)
declare @fsrccommitfield_endvalue decimal(28,10)
declare @maxorder int
update src set @fsrccommitfield_prevalue= isnull(abs(src.finvoiceamtfor_relative),0),
@fsrccommitfield_endvalue=@fsrccommitfield_prevalue+dest.famountincludetax,
@fcheck_fail=case isnull(@maxorder,0) when 1 then 0 else (case when (1=1) then @fcheck_fail else -1 end) end,
src.finvoiceamtfor_relative= case when src.famountincludetax < 0 then -1 * @fsrccommitfield_endvalue else @fsrccommitfield_endvalue end
from t_rpcontractentry src
inner join t_rpcontract srchead on src.fcontractid=srchead.fcontractid
inner join
(select u1.fcontractinterid as fsourceinterid,u1.fcontractentryid,u1.fitemid,sum(u1.famountincludetax) as famountincludetax
from icpurchaseentry u1
where u1.finterid=1204
and not exists (select 1 from icbillrelations_purchase tr where u1.finterid = tr.fbillid and u1.fentryid=tr.fdestentryid) and not exists (select 1 from icbillrelations_purchaser tr where u1.finterid = tr.fbillid and u1.fentryid=tr.fdestentryid) group by u1.fcontractinterid,u1.fcontractentryid,u1.fitemid) dest
on dest.fsourceinterid = src.fcontractid
and dest.fitemid = src.fproductid
and src.fentryid = dest.fcontractentryid
where (select fclasstypeid from t_rpcontract where fcontractid=src.fcontractid)=1000551
--更新合同(应付)的收款关联金额 (本位币)
set nocount on
declare @fcheck_fail int
declare @fsrccommitfield_prevalue decimal(28,13)
declare @fsrccommitfield_endvalue decimal(28,10)
declare @maxorder int
update src set @fsrccommitfield_prevalue= isnull(abs(src.freceiveamount),0),
@fsrccommitfield_endvalue=@fsrccommitfield_prevalue+dest.fstdamountincludetax,
@fcheck_fail=case isnull(@maxorder,0) when 1 then 0 else (case when (1=1) then @fcheck_fail else -1 end) end,
src.freceiveamount= case when src.famountincludetax < 0 then -1 * @fsrccommitfield_endvalue else @fsrccommitfield_endvalue end
from t_rpcontractentry src
inner join t_rpcontract srchead on src.fcontractid=srchead.fcontractid
inner join
(select u1.fcontractinterid as fsourceinterid,u1.fcontractentryid,u1.fitemid,sum(u1.fstdamountincludetax) as fstdamountincludetax
from icpurchaseentry u1
where u1.finterid=1204
and not exists (select 1 from icbillrelations_purchase tr where u1.finterid = tr.fbillid and u1.fentryid=tr.fdestentryid) and not exists (select 1 from icbillrelations_purchaser tr where u1.finterid = tr.fbillid and u1.fentryid=tr.fdestentryid) and (select fpostyle from icpurchase where finterid=u1.finterid)= 251 and (select fclasstypeid from t_rpcontract where fcontractid=u1.fcontractinterid)=1000020 group by u1.fcontractinterid,u1.fcontractentryid,u1.fitemid) dest
on dest.fsourceinterid = src.fcontractid
and dest.fitemid = src.fproductid
and src.fentryid = dest.fcontractentryid
--更新合同(应付)的发票关联金额
set nocount on
declare @fcheck_fail int
declare @fsrccommitfield_prevalue decimal(28,13)
declare @fsrccommitfield_endvalue decimal(28,10)
declare @maxorder int
update src set @fsrccommitfield_prevalue= isnull(abs(src.freceiveamountfor),0),
@fsrccommitfield_endvalue=@fsrccommitfield_prevalue+dest.famountincludetax,
@fcheck_fail=case isnull(@maxorder,0) when 1 then 0 else (case when (1=1) then @fcheck_fail else -1 end) end,
src.freceiveamountfor= case when src.famountincludetax < 0 then -1 * @fsrccommitfield_endvalue else @fsrccommitfield_endvalue end
from t_rpcontractentry src
inner join t_rpcontract srchead on src.fcontractid=srchead.fcontractid
inner join
(select u1.fcontractinterid as fsourceinterid,u1.fcontractentryid,u1.fitemid,sum(u1.famountincludetax) as famountincludetax
from icpurchaseentry u1
where u1.finterid=1204
and not exists (select 1 from icbillrelations_purchase tr where u1.finterid = tr.fbillid and u1.fentryid=tr.fdestentryid) and not exists (select 1 from icbillrelations_purchaser tr where u1.finterid = tr.fbillid and u1.fentryid=tr.fdestentryid) and (select fpostyle from icpurchase where finterid=u1.finterid)= 251 and (select fclasstypeid from t_rpcontract where fcontractid=u1.fcontractinterid)=1000020 group by u1.fcontractinterid,u1.fcontractentryid,u1.fitemid) dest
on dest.fsourceinterid = src.fcontractid
and dest.fitemid = src.fproductid
and src.fentryid = dest.fcontractentryid
--更新合同(应付)的收款执行金额(本位币)
set nocount on
declare @fcheck_fail int
declare @fsrccommitfield_prevalue decimal(28,13)
declare @fsrccommitfield_endvalue decimal(28,10)
declare @maxorder int
update src set @fsrccommitfield_prevalue= isnull(abs(src.fbillamt_commit),0),
@fsrccommitfield_endvalue=@fsrccommitfield_prevalue+dest.fstdamountincludetax,
@fcheck_fail=case isnull(@maxorder,0) when 1 then 0 else (case when (1=1) then @fcheck_fail else -1 end) end,
src.fbillamt_commit= case when src.famountincludetax < 0 then -1 * @fsrccommitfield_endvalue else @fsrccommitfield_endvalue end
from t_rpcontractentry src
inner join t_rpcontract srchead on src.fcontractid=srchead.fcontractid
inner join
(select u1.fcontractinterid as fsourceinterid,u1.fcontractentryid,u1.fitemid,sum(u1.fstdamountincludetax) as fstdamountincludetax
from icpurchaseentry u1
where u1.finterid=1204
and not exists (select 1 from icbillrelations_purchase tr where u1.finterid = tr.fbillid and u1.fentryid=tr.fdestentryid) and not exists (select 1 from icbillrelations_purchaser tr where u1.finterid = tr.fbillid and u1.fentryid=tr.fdestentryid) and (select fpostyle from icpurchase where finterid=u1.finterid)= 251 and (select fclasstypeid from t_rpcontract where fcontractid=u1.fcontractinterid)=1000551 group by u1.fcontractinterid,u1.fcontractentryid,u1.fitemid) dest
on dest.fsourceinterid = src.fcontractid
and dest.fitemid = src.fproductid
and src.fentryid = dest.fcontractentryid
--更新合同(应付)
set nocount on
declare @fcheck_fail int
declare @fsrccommitfield_prevalue decimal(28,13)
declare @fsrccommitfield_endvalue decimal(28,10)
declare @maxorder int
update src set @fsrccommitfield_prevalue= isnull(abs(src.fbillamtfor_commit),0),
@fsrccommitfield_endvalue=@fsrccommitfield_prevalue+dest.famountincludetax,
@fcheck_fail=case isnull(@maxorder,0) when 1 then 0 else (case when (1=1) then @fcheck_fail else -1 end) end,
src.fbillamtfor_commit= case when src.famountincludetax < 0 then -1 * @fsrccommitfield_endvalue else @fsrccommitfield_endvalue end
from t_rpcontractentry src
inner join t_rpcontract srchead on src.fcontractid=srchead.fcontractid
inner join
(select u1.fcontractinterid as fsourceinterid,u1.fcontractentryid,u1.fitemid,sum(u1.famountincludetax) as famountincludetax
from icpurchaseentry u1
where u1.finterid=1204
and not exists (select 1 from icbillrelations_purchase tr where u1.finterid = tr.fbillid and u1.fentryid=tr.fdestentryid) and not exists (select 1 from icbillrelations_purchaser tr where u1.finterid = tr.fbillid and u1.fentryid=tr.fdestentryid) and (select fpostyle from icpurchase where finterid=u1.finterid)= 251 and (select fclasstypeid from t_rpcontract where fcontractid=u1.fcontractinterid)=1000551 group by u1.fcontractinterid,u1.fcontractentryid,u1.fitemid) dest
on dest.fsourceinterid = src.fcontractid
and dest.fitemid = src.fproductid
and src.fentryid = dest.fcontractentryid
--更新外购入库的辅助单位开票数量
set nocount on
declare @fcheck_fail int
declare @fsrccommitfield_prevalue decimal(28,13)
declare @fsrccommitfield_endvalue decimal(28,10)
declare @maxorder int
update src set @fsrccommitfield_prevalue= isnull(src.fsecinvoiceqty,0),
@fsrccommitfield_endvalue=@fsrccommitfield_prevalue+dest.fsecqty,
@fcheck_fail=case isnull(@maxorder,0) when 1 then 0 else (case when (1=1) then @fcheck_fail else -1 end) end,
src.fsecinvoiceqty=@fsrccommitfield_endvalue
from icstockbillentry src
inner join icstockbill srchead on src.finterid=srchead.finterid
inner join
(select u1.fsourceinterid as fsourceinterid,u1.fsourceentryid,u1.fitemid,sum(u1.fsecqty) as fsecqty
from icpurchaseentry u1
where u1.finterid=1204
group by u1.fsourceinterid,u1.fsourceentryid,u1.fitemid) dest
on dest.fsourceinterid = src.finterid
and dest.fitemid = src.fitemid
and src.fentryid = dest.fsourceentryid
--更新采购订单的辅助单位开票数量
set nocount on
declare @fcheck_fail int
declare @fsrccommitfield_prevalue decimal(28,13)
declare @fsrccommitfield_endvalue decimal(28,10)
declare @maxorder int
update src set @fsrccommitfield_prevalue= isnull(src.fsecinvoiceqty,0),
@fsrccommitfield_endvalue=@fsrccommitfield_prevalue+dest.fsecqty,
@fcheck_fail=case isnull(@maxorder,0) when 1 then 0 else (case when (1=1) then @fcheck_fail else -1 end) end,
src.fsecinvoiceqty=@fsrccommitfield_endvalue
from poorderentry src
inner join poorder srchead on src.finterid=srchead.finterid
inner join
(select u1.forderinterid as fsourceinterid,u1.forderentryid,u1.fitemid,sum(u1.fsecqty) as fsecqty
from icpurchaseentry u1
where u1.finterid=1204
group by u1.forderinterid,u1.forderentryid,u1.fitemid) dest
on dest.fsourceinterid = src.finterid
and dest.fitemid = src.fitemid
and src.fentryid = dest.forderentryid
--更新合同(应付)
set nocount on
declare @fcheck_fail int
declare @fsrccommitfield_prevalue decimal(28,13)
declare @fsrccommitfield_endvalue decimal(28,10)
declare @maxorder int
update src set @fsrccommitfield_prevalue= isnull(abs(src.fsecinvoicecommitqty),0),
@fsrccommitfield_endvalue=@fsrccommitfield_prevalue+dest.fsecqty,
@fcheck_fail=case isnull(@maxorder,0) when 1 then 0 else (case when (1=1) then @fcheck_fail else -1 end) end,
src.fsecinvoicecommitqty= case when src.fsecqty < 0 then -1 * @fsrccommitfield_endvalue else @fsrccommitfield_endvalue end
from t_rpcontractentry src
inner join t_rpcontract srchead on src.fcontractid=srchead.fcontractid
inner join
(select u1.fcontractinterid as fsourceinterid,u1.fcontractentryid,u1.fitemid,sum(u1.fsecqty) as fsecqty
from icpurchaseentry u1
where u1.finterid=1204
group by u1.fcontractinterid,u1.fcontractentryid,u1.fitemid) dest
on dest.fsourceinterid = src.fcontractid
and dest.fitemid = src.fproductid
and src.fentryid = dest.fcontractentryid
where (select fclasstypeid from t_rpcontract where fcontractid=src.fcontractid)=1000020
--更新合同(应付)
set nocount on
declare @fcheck_fail int
declare @fsrccommitfield_prevalue decimal(28,13)
declare @fsrccommitfield_endvalue decimal(28,10)
declare @maxorder int
update src set @fsrccommitfield_prevalue= isnull(abs(src.fsecinvoicecommitqty),0),
@fsrccommitfield_endvalue=@fsrccommitfield_prevalue+dest.fsecqty,
@fcheck_fail=case isnull(@maxorder,0) when 1 then 0 else (case when (1=1) then @fcheck_fail else -1 end) end,
src.fsecinvoicecommitqty= case when src.fsecqty < 0 then -1 * @fsrccommitfield_endvalue else @fsrccommitfield_endvalue end
from t_rpcontractentry src
inner join t_rpcontract srchead on src.fcontractid=srchead.fcontractid
inner join
(select u1.fcontractinterid as fsourceinterid,u1.fcontractentryid,u1.fitemid,sum(u1.fsecqty) as fsecqty
from icpurchaseentry u1
where u1.finterid=1204
group by u1.fcontractinterid,u1.fcontractentryid,u1.fitemid) dest
on dest.fsourceinterid = src.fcontractid
and dest.fitemid = src.fproductid
and src.fentryid = dest.fcontractentryid
where (select fclasstypeid from t_rpcontract where fcontractid=src.fcontractid)=1000551
--更新VMI物料消耗结算单表体汇总
set nocount on
declare @fcheck_fail int
declare @fsrccommitfield_prevalue decimal(28,13)
declare @fsrccommitfield_endvalue decimal(28,10)
declare @maxorder int
update src set @fsrccommitfield_prevalue= isnull(src.fqtyinvoice,0),
@fsrccommitfield_endvalue=@fsrccommitfield_prevalue+dest.fqty,
@fcheck_fail=case isnull(@maxorder,0) when 1 then 0 else (case when (abs(src.fqty)>=abs(@fsrccommitfield_endvalue)) then @fcheck_fail else -1 end) end,
src.fqtyinvoice=@fsrccommitfield_endvalue,
src.fauxqtyinvoice=@fsrccommitfield_endvalue/cast(t1.fcoefficient as float)
from icitemaccountcheckentrysum src
inner join icitemaccountcheck srchead on src.fid=srchead.fid
inner join
(select u1.fitemstatementinterid as fsourceinterid,u1.fitemstatemententryid,u1.fitemid,sum(u1.fqty) as fqty
from icpurchaseentry u1
where u1.finterid=1204
group by u1.fitemstatementinterid,u1.fitemstatemententryid,u1.fitemid) dest
on dest.fsourceinterid = src.fid
and dest.fitemid = src.fitemid
and src.fentryid = dest.fitemstatemententryid
inner join t_measureunit t1 on src.funitid=t1.fitemid
if (isnull(@fcheck_fail,0)=-1)
raiserror('可能的原因是:
1、所选单据已被其他单据关联
2、所选单据已被反审核
3、当前单据和所选单据的关联数量超过了所选单据的数量
4、所选单据已经关闭',18,18)
else
if exists (select 1 from icitemaccountcheck src right join (select u1.fitemstatementinterid as fsourceinterid,u1.fitemstatemententryid,u1.fitemid,sum(u1.fqty) as fqty
from icpurchaseentry u1
where u1.finterid=1204
group by u1.fitemstatementinterid,u1.fitemstatemententryid,u1.fitemid) dest on dest.fsourceinterid = src.fid where dest.fsourceinterid>0 and src.fid is null)
raiserror('所选单据已被删除',18,18)
--更新外购入库的
set nocount on
declare @fcheck_fail int
declare @fsrccommitfield_prevalue decimal(28,13)
declare @fsrccommitfield_endvalue decimal(28,10)
declare @maxorder int
update src set @fsrccommitfield_prevalue= isnull(src.fcommitamt,0),
@fsrccommitfield_endvalue=@fsrccommitfield_prevalue+dest.famount,
@fcheck_fail=case isnull(@maxorder,0) when 1 then 0 else (case when (1=1) then @fcheck_fail else -1 end) end,
src.fcommitamt=@fsrccommitfield_endvalue
from icstockbillentry src
inner join icstockbill srchead on src.finterid=srchead.finterid
inner join
(select u1.fsourceinterid as fsourceinterid,u1.fsourceentryid,u1.fitemid,sum(u1.famount) as famount
from icpurchaseentry u1
where u1.finterid=1204
group by u1.fsourceinterid,u1.fsourceentryid,u1.fitemid) dest
on dest.fsourceinterid = src.finterid
and dest.fitemid = src.fitemid
and src.fentryid = dest.fsourceentryid
IF EXISTS (SELECT 1 FROM ICBillRelations_Purchase WHERE FBillType = 75 AND FBillID=1204)
BEGIN
UPDATE t1 SET t1.FChildren=t1.FChildren+1
FROM ICStockBill t1 INNER JOIN ICStockBillEntry t2 ON t1.FInterID=t2.FInterID
INNER JOIN ICBillRelations_Purchase t3 ON t3.FMultiEntryID=t2.FEntryID AND t3.FMultiInterID=t2.FInterID
WHERE t3.FBillType=75 AND t3.FBillID=1204
END
ELSE
BEGIN
UPDATE t3 SET t3.FChildren=t3.FChildren+1
FROM ICPurchase t1 INNER JOIN ICPurchaseEntry t2 ON t1.FInterID=t2.FInterID
INNER JOIN ICStockBill t3 ON t3.FTranType=t2.FSourceTranType AND t3.FInterID=t2.FSourceInterID
WHERE t1.FTranType=75 AND t1.FInterID=1204 AND t2.FSourceInterID>0
END
--更新合同(应付)
UPDATE t_rpContractEntry SET FInvoiceAmtFor_Relative = round((u.FInvoiceAmtFor_Relative +(1)*t1.FQty*t4.FPriceDiscount),isnull(t6.FScale,2)),
FInvoiceAmt_Relative = round((u.FInvoiceAmt_Relative +(1)* t1.FQty * t4.FPriceDiscount * t3.FExchangeRate),isnull(t6.FScale,2)),
FReceiveAmountFor =CASE WHEN t3.FPOStyle=251 THEN round((u.FReceiveAmountFor +(1)* t1.FQty*t4.FPriceDiscount),isnull(t6.FScale,2)) ELSE u.FReceiveAmountFor END,
FReceiveAmount = CASE WHEN t3.FPOStyle=251 THEN round((u.FReceiveAmount +(1)* t1.FQty * t4.FPriceDiscount * t3.FExchangeRate),isnull(t6.FScale,2)) ELSE u.FReceiveAmount END
FROM t_rpContractEntry u
join t_rpContract v on v.FContractID = u.FContractID
join ICStockBillEntry t2 on u.FEntryID = t2.FContractEntryID
join ICBillRelations_Purchase t1 on t1.FMultiInterID = t2.FInterID and t1.FMultiEntryID = t2.FEntryID
join ICPurchase t3 on t1.FBillID=t3.FInterID
join ICPurchaseEntry t4 on t1.FBillID=t4.FInterID and t1.FDestEntryID=t4.FEntryID
LEFT JOIN t_Currency t6 ON v.FCurrencyID=t6.FCurrencyID
WHERE t1.FBillID =1204
--更新合同(应付)
UPDATE t_rpContractEntry SET FInvoiceAmtFor_Relative = round((u.FInvoiceAmtFor_Relative -(1)* t1.FQty*t4.FPriceDiscount),isnull(t6.FScale,2)),
FInvoiceAmt_Relative = round((u.FInvoiceAmt_Relative -(1)* t1.FQty * t4.FPriceDiscount * t3.FExchangeRate),isnull(t6.FScale,2)),
FInvoiceQty_Relative = round((u.FInvoiceQty_Relative -(1)* t1.FQty),isnull(t7.FQtyDecimal,4)),
FInvoiceQty_Relative_Base = round((u.FInvoiceQty_Relative_Base -(1)* t1.FQty*tu.FCoefficient),isnull(t7.FQtyDecimal,4)),
FSecInvoiceCommitQty=round((u.FSecInvoiceCommitQty-(1)*t1.FQty/t4.FSecCoefficient),isnull(t7.FQtyDecimal,4)),
FReceiveAmountFor =CASE WHEN t3.FPOStyle=251 THEN round((u.FReceiveAmountFor -(1)* t1.FQty*t4.FPriceDiscount),isnull(t6.FScale,2)) ELSE u.FReceiveAmountFor END,
FReceiveAmount = CASE WHEN t3.FPOStyle=251 THEN round((u.FReceiveAmount -(1)* t1.FQty * t4.FPriceDiscount * t3.FExchangeRate),isnull(t6.FScale,2)) ELSE u.FReceiveAmount END
FROM t_rpContractEntry u
join t_rpContract v on v.FContractID = u.FContractID
join ICStockBillEntry t2 on u.FEntryID = t2.FContractEntryID
join ICBillRelations_PurchaseR t1 on t1.FMultiInterID = t2.FInterID and t1.FMultiEntryID = t2.FEntryID and t1.FSourceTranType in (1,5)
join ICPurchase t3 on t1.FBillID=t3.FInterID
join ICPurchaseEntry t4 on t1.FBillID=t4.FInterID and t1.FDestEntryID=t4.FEntryID
left join t_MeasureUnit tu on t4.FUnitID=tu.FMeasureUnitID
LEFT JOIN t_Currency t6 ON v.FCurrencyID=t6.FCurrencyID
LEFT JOIN t_ICItem t7 ON u.FProductID=t7.FItemID
WHERE t1.FBillID =1204
--更新采购订单
Update Src
Set Src.FReceiveAmountFor_Commit=Src.FReceiveAmountFor_Commit + (IsNull(Dest.FCommitAmtFor, 0) * IsNull(Dest.FExchangeRate, 1) / IsNull(Head.FExchangeRate, 1)),
Src.FReceiveAmount_Commit=Src.FReceiveAmount_Commit+ISNull(Dest.FCommitAmt,0)
From POOrderEntry Src Inner Join POOrder Head On Src.FInterID=Head.FInterID
Inner Join (
Select isnull(ti.FOrderInterID,t1.FOrderInterID) as FOrderInterID,isnull(ti.FOrderEntryID,t1.FOrderEntryID) as FOrderEntryID,t1.Fitemid,
sum(case when ti.FOrderInterID is null then (Case t2.FTranType When 75 Then t1.FAmountIncludeTax When 76 Then t1.FAmount Else 0 End) else ti.FQty*(Case t2.FTranType When 75 Then t1.FAmountIncludeTax When 76 Then t1.FAmount Else 0 End)/t1.FQty end) as FCommitAmtFor,
sum(case when ti.FOrderInterID is null then (Case t2.FTranType When 75 Then t1.FStdAmountIncludeTax When 76 Then t1.FStdAmount Else 0 End) else ti.FQty*(Case t2.FTranType When 75 Then t1.FStdAmountIncludeTax When 76 Then t1.FStdAmount Else 0 End)/t1.FQty end) as FCommitAmt,
t2.FExchangeRate
From ICPurchaseEntry t1 left Join ICPurchase t2 On t1.FInterID=t2.FInterID
left join (select a.fbillid,a.FDestEntryID,b.FOrderInterID,b.FOrderEntryID,a.fqty from ICBillRelations_Purchase a
join ICStockBillEntry b on a.FMultiInterID=b.FInterID and a.FMultiEntryID=b.fentryid ) ti on t1.FInterID=ti.FBillID and t1.FEntryID=ti.FDestEntryID Where t2.FTranType in (75,76) and t2.FPOStyle=251 and t1.FOrderInterID>0 and t2.FInterID= 1204
Group by isnull(ti.FOrderInterID,t1.FOrderInterID),isnull(ti.FOrderEntryID,t1.FOrderEntryID), t1.FItemID,t2.FTranType,t2.FExchangeRate
) Dest on Dest.FOrderInterID = Src.FInterID
and Dest.Fitemid = Src.Fitemid and Src.Fentryid = Dest.FOrderEntryID
--更新合同收款计划明细
update t_rpContractScheme set FReceiveAmountFor = t1.FReceiveAmountFor - t2.FReceiveAmountFor
From t_rpContractScheme t1,
(select FContractID,FSchemeEntryID,sum(FAmountFor) FReceiveAmountFor from t_rp_ContractPlanReceive where FBillID=1204 and FBillType = 4 group by FContractID,FSchemeEntryID ) t2
where t1.FContractID=t2.FContractID and t1.FEntryID=t2.FSchemeEntryID
delete t_rp_ContractPlanReceive where FBillID = 1204 and FBillType = 4
update ICPurchase set FFincDate=FDate, FYear=2018, FPeriod=4 where FinterID=1204
update ICPurchase set FFincDate=FDate, FYear=2018, FPeriod=4 where FinterID=1204Update e
SET e.FRemainAmount=(case h.FClassTypeID when 1000003 then e.FStdAmount else e.FStdAmountincludetax end),
e.FRemainAmountFor=(case h.FClassTypeID when 1000003 then e.FAmount else e.FAmountincludetax end),
e.FRemainQty=e.FAuxQty,e.FClassID_SRC=e.FSourceTranType
from ICPurchaseEntry e join ICPurchase h on e.FinterID=h.FinterID
Where e.FinterID = 1204
Update t1
set t1.FAdjustExchangeRate=case when t2.FOperator='/' then 1/t1.FExchangerate else t1.FExchangerate end,
t1.FCheckStatus=(case t1.FPoStyle when 251 then 1 else 0 end),t1.FArapStatus=(case t1.FPoStyle when 251 then 4 else 0 end)
from ICPurchase t1
inner join t_Currency t2 on t1.FCurrencyID=t2.FCurrencyID
where t1.FinterID = 1204
--新增付款计划
insert into t_rp_plan_ap(FOrgID,FDate,FAmount,FAmountFor, FRemainAmount,FRemainAmountFor,FRP,FinterID)
values(0,'2018-05-08',262167.56,262167.56,262167.56,262167.56,0,1204)
--应收、应付往来表
exec sp_executesql N'INSERT INTO t_RP_Contact (FYear,FPeriod,FRP,FType,FDate,FFincDate,FNumber,FCustomer,FDepartment,FEmployee,FCurrencyID,FExchangeRate,FAmount,FAmountFor,FRemainAmount,FRemainAmountFor,FInvoiceID,FRPDate,FK3Import,FInterestRate,FBillType,finvoicetype,FItemClassID,FExplanation,FPreparer) VALUES (@P1,@P2,@P3,@P4,@P5,@P6,@P7,@P8,@P9,@P10,@P11,@P12,@P13,@P14,@P15,@P16,@P17,@P18,@P19,@P20,@P21,@P22,@P23,@P24,@P25)',N'@P1 int,@P2 int,@P3 smallint,@P4 smallint,@P5 datetime,@P6 datetime,@P7 varchar(255),@P8 int,@P9 int,@P10 int,@P11 int,@P12 float,@P13 money,@P14 money,@P15 money,@P16 money,@P17 int,@P18 datetime,@P19 smallint,@P20 float,@P21 int,@P22 smallint,@P23 int,@P24 varchar(255),@P25 int',2018,4,0,4,'2018-04-10 00:00:00','2018-04-10 00:00:00','ZPOFP000095',412,241,2188,1,1,$262167.5600,$262167.5600,$262167.5600,$262167.5600,1204,'2018-04-10 00:00:00',1,0,1,2,8,'',16394
update t_rp_plan_ap set FOrgID=338 where FIsinit=0 and FinterID=1204 and FRP=0
--最后交易金额
update t_supplier set FLastTradeDate='2018-04-10', FLastTradeAmount=262167.56 where FItemID=412
Update ICPurchase Set FSubSystemID=0 Where Ftrantype in(75,76) and FInterID=1204
Update v1 set v1.FOrderPrice=round(u1.FPrice *w1.FExchangeRate*(100-u1.FDiscountRate)/100,t1.FPriceDecimal)
From t_ICItemCore v1
Right Join ICPurchaseEntry u1 On v1.FItemID=u1.FItemID
Left Join ICPurchase w1 On u1.FInterID=w1.FInterID
Left Join t_ICItem t1 On t1.FItemID =u1.FItemID
Where u1.finterid=1204
And u1.FEntryID=(Select Max(FEntryid) From ICPurchaseEntry Where FInterid=u1.FInterid and FItemid=u1.FItemid)
UPDATE ICPurchaseEntry SET FAmountMustOld=FAmountMust,FNoMustOld=FNoMust,FDeductTaxOld=FDeductTax WHERE FInterID=1204
update ICStockBill set FRelateInvoiceID=1204 where FInterID=2199
INSERT INTO t_Log (FDate,FUserID,FFunctionID,FStatement,FDescription,FMachineName,FIPAddress) VALUES (getdate(),16394,'K000301',3,'编号为ZPOFP000095的单据保存成功','WIN-5579AATH4RN','192.168.6.149')
update icpurchase set FCheckerID=16394 ,FArApStatus=FArApStatus | 1 ,fcheckdate=getdate() where FInterID=1204;
update t_RP_Contact set FStatus=FStatus | 1,FToBal=1 where FInvoiceID=1204 and FType = 4 and FK3Import=1
UPDATE ICPurchase SET FCheckerID=16394,FStatus=1
,FCheckDate='2018-04-10' WHERE FInterID=1204