
-- 以下收付处理脚本置于所有业务组单据升级脚本的最后
-- U870及之前版本不支持收付款协议档案
if exists (select name from @t6db..syscolumns where id=
	(select top 1 id from @t6db..sysobjects where name ='AP_Vouch' and XTYPE ='U') and name=N'cAccountPDate')	
		and exists (select name from @u8db..syscolumns where id=
		(select top 1 id from @u8db..sysobjects where name ='AP_Vouch' and XTYPE ='U') and name=N'cGatheringPlan')
begin
	exec('declare @cGatheringPlan_Visual varchar(10)   
	set @cGatheringPlan_Visual = ''999''

	-- 更新单据数据(应收应付单)
	update a set cGatheringPlan = @cGatheringPlan_Visual,dCreditStart = a.dVouchDate,iCreditPeriod = 0,dGatheringDate=b.cAccountPDate
	from @u8db..AP_Vouch a inner join @t6db..AP_Vouch b on a.cFlag = b.cFlag and a.cVouchType = b.cVouchType and a.Auto_ID = b.Auto_ID
	where isnull(b.cAccountPDate,'''') <>''''

	-- 更新记账数据(AP_Vouch)
	update b set cGatheringPlan = a.cGatheringPlan,dCreditStart = a.dCreditStart,
			iCreditPeriod = 0, dGatheringDate=a.dGatheringDate,bCredit = 1
	from @u8db..AP_Vouch a inner join @u8db..AP_detail b on a.cVouchType = b.cCoVouchType and a.cVouchID = b.cCoVouchID
	where a.cFlag = ''AP'' and b.cCoVouchType like ''P%'' AND isnull(a.cGatheringPlan,'''') <>''''

	if not exists(select name from @u8db..sysobjects where name =N''AR_detail'' and xtype=N''U'')
		-- U890及之前版本不存在 AR_detail 表
		update b set cGatheringPlan = a.cGatheringPlan,dCreditStart = a.dCreditStart,
				iCreditPeriod = 0, dGatheringDate=a.dGatheringDate,bCredit = 1
		from @u8db..AP_Vouch a inner join @u8db..AP_detail b on a.cVouchType = b.cCoVouchType and a.cVouchID = b.cCoVouchID
		where a.cFlag = ''AR'' and b.cFlag = ''AR'' and b.cCoVouchType like ''R%'' AND isnull(a.cGatheringPlan,'''') <>''''
	else
		update b set cGatheringPlan = a.cGatheringPlan,dCreditStart = a.dCreditStart,
				iCreditPeriod = 0, dGatheringDate=a.dGatheringDate,bCredit = 1
		from @u8db..AP_Vouch a inner join @u8db..AR_detail b on a.cVouchType = b.cCoVouchType and a.cVouchID = b.cCoVouchID
		where a.cFlag = ''AR'' and b.cCoVouchType like ''R%'' AND isnull(a.cGatheringPlan,'''') <>''''


	-- 更新记账数据(销售发票)
	if not exists(select name from @u8db..sysobjects where name =N''AR_detail'' and xtype=N''U'')
		-- U890及之前版本不存在 AR_detail 表
		update b set cGatheringPlan = @cGatheringPlan_Visual,dCreditStart = a.dDate,
				iCreditPeriod = 0, dGatheringDate=a.cAccountPDate,bCredit = 1
		from @t6db..SaleBillVouch a inner join @u8db..AP_detail b on a.cVouchType = b.cCoVouchType and a.cSBVCode = b.cCoVouchID
		where b.cFlag = ''AR'' and b.cCoVouchType like ''2%'' AND isnull(a.cAccountPDate,'''') <>''''
	else
		update b set cGatheringPlan = @cGatheringPlan_Visual,dCreditStart = a.dDate,
				iCreditPeriod = 0, dGatheringDate=a.cAccountPDate,bCredit = 1
		from @t6db..SaleBillVouch a inner join @u8db..AR_detail b on a.cVouchType = b.cCoVouchType and a.cSBVCode = b.cCoVouchID
		where b.cCoVouchType like ''2%'' AND isnull(a.cAccountPDate,'''') <>''''

	-- 更新记账数据(采购发票)
	update b set cGatheringPlan = @cGatheringPlan_Visual,dCreditStart = a.dPBVDate,
			iCreditPeriod = 0, dGatheringDate=a.cAccountPDate,bCredit = 1
	from @t6db..purbillvouch a inner join @u8db..AP_detail b on a.cPBVBillType = b.cCoVouchType and a.cPBVCode = b.cCoVouchID
	where b.cCoVouchType like ''0%'' AND isnull(a.cAccountPDate,'''') <>''''

	-- 委外发票处理
	-- 委外发票单据数据不做升级,记账数据和凭证保留;
	--update @u8db..AP_detail set cvouchtype = ''0''+ltrim(str(CONVERT(int,cVouchType) - 10)) where cVouchType in (''11'',''12'',''13'')
	--update @u8db..AP_detail set cCoVouchType = ''0''+ltrim(str(CONVERT(int,cCoVouchType) - 10)) where cCoVouchType in (''11'',''12'',''13'')
	--update @u8db..AP_detail set cProcStyle = ''0''+ltrim(str(CONVERT(int,cProcStyle) - 10)) where cProcStyle in (''11'',''12'',''13'')
	-- cTypeCode与U8定义不同
	delete from @u8db..ap_vouchtype where cTypeCode in (''11'',''12'',''13'')

	')
end
GO
