SBO帐龄分析

/******************************************************
*name --函数名 : HBU_Reports_Aging
*function --函数功能 : 生成帐龄分析报表
*input --输入参数 : @ReportsDate datetime --生成帐龄分析报表的日期
*output --输出参数 : 帐龄分析报表
*author --作 者 : 阚山
*CreateDate --创建时间 : 20060528
*UpdateDate --函数更改信息(包括作者、时间、更改内容等)
**************************************************/

--生成帐龄分析报表_存储过程的SQL语句--
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'HBU_Reports_Aging' AND type = 'P')
DROP PROCEDURE HBU_Reports_Aging
GO
--Tmsp_HBU 集团财务SDK开发存储过程名--
--Reports_Aging 帐龄分析报表--
CREATE PROC HBU_Reports_Aging
@ReportsDate datetime --生成帐龄分析报表的日期
--/创建帐龄分析报表存储过程HBU_Reports_Aging/--
WITH ENCRYPTION --加密存储过程--
AS
--判断是否存在表[@HBU6ZLFX],不存在新建表结构--
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[@HBU6ZLFX]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
PRINT '[@HBU6ZLFX]已经存在'
else
BEGIN
CREATE TABLE [dbo].[@HBU6ZLFX] (
[CardCode] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[CardName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[AcctCode] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[Total] [numeric](18, 2) NULL ,
[0-30] [numeric](18, 2) NULL ,
[30-60] [numeric](18, 2) NULL ,
[60-90] [numeric](18, 2) NULL ,
[90-++] [numeric](18, 2) NULL ,
[AddDate] [datetime] NULL ,
[ReportDate] [datetime] NULL
) ON [PRIMARY]
END
--判断是否已经生成过,生成过先删除--
if (select count(*) from [@HBU6ZLFX] where year(ADDDate) = year(@ReportsDate) and month(ADDDate) = month(@ReportsDate))>$0
BEGIN
delete from [@HBU6ZLFX] where year(ADDDate) = year(@ReportsDate) and month(ADDDate) = month(@ReportsDate)
PRINT '已经删除了生成已经生成的数据'

END

--向表[@HBU6ZLFX]插入当月的帐龄分析数据--
INSERT into [@HBU6ZLFX]
select TMP_ZL.ShortName as 客户号,OCRD.cardname as 客户名,TMP_ZL.Account as 科目号,sum(TMP_ZL.发生额) as 余额,
sum(TMP_ZL.帐期30天内) as '0-30',sum(TMP_ZL.帐期60天内)as '30-60',sum(TMP_ZL.帐期90天内)as '60-90',sum(TMP_ZL.帐期90天以上)as '90++',
getdate() as ADDDate,@ReportsDate from
(SELECT T1.ShortName , 发生额=T1.Debit-T1.Credit, T1.Account,
帐期30天内=case when T1.DueDate >= @ReportsDate-30 and T1.DueDate <=getdate() then (T1.Debit-T1.Credit) else 0 end
,帐期60天内=case when T1.DueDate >= @ReportsDate-60 and T1.DueDate < @ReportsDate-30 then (T1.Debit-T1.Credit) else 0 end
,帐期90天内=case when T1.DueDate >= @ReportsDate-90 and T1.DueDate < @ReportsDate-60 then (T1.Debit-T1.Credit) else 0 end
,帐期90天以上=case when T1.DueDate < @ReportsDate-90 then (T1.Debit-T1.Credit) else 0 end

FROM [JDT1] T1
WHERE (left (T1.ShortName,1)='C' OR left (T1.ShortName,1)='S') AND T1.IntrnMatch = 0 )as TMP_ZL,OCRD
where OCRD.cardcode=TMP_ZL.ShortName
group by TMP_ZL.ShortName,TMP_ZL.Account,OCRD.cardname
HAVING sum(TMP_ZL.发生额)<>0
--向表[@HBU6ZLFX]插入当月数据结束--

--输出当月帐龄分析报表--
select * from [@HBU6ZLFX] where year(ReportDate) = year(@ReportsDate) and month(ReportDate) = month(@ReportsDate)

你可能感兴趣的:(职场,休闲,Sbo,SBO帐龄分析,SBO帐龄,sbo软件)